﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TInventoryDetailRepository : BaseRepository<TInventoryDetail>, ITInventoryDetailRepository
    {
        public TInventoryDetailRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        public List<TInventoryDetailDto> GetInventoryDetailPageByPalletPackageBarcode(out int totalCount, string PalletBarcode, string? PackageBarcode, int pageIndex, int pageSize)
        {
            var query = Context.TInventoryDetails.Where(s => s.PalletBarcode == PalletBarcode);

            if (!string.IsNullOrEmpty(PackageBarcode))
            {
                query = query.Where(s => s.PackageBarcode.Contains(PackageBarcode));
            }

            totalCount = query.Count();

            var result = query
                .OrderBy(s => s.Id)
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize)
                .Select(s => new TInventoryDetailDto()
                {
                    PackageBarcode = s.PackageBarcode,
                    Barcode = s.Barcode,
                })
                .ToList();

            return result;
        }

    }
}
